import { Product } from '../types';
import { Storage, STORAGE_KEYS } from '../utils/storage';
import { mockProducts } from './mockData';

export class SearchService {
  static searchProducts(keyword: string, categoryId?: string): Product[] {
    const products = Storage.get<Product[]>(STORAGE_KEYS.PRODUCTS) || mockProducts;
    
    if (!keyword && !categoryId) {
      return products;
    }

    return products.filter(product => {
      const matchesKeyword = !keyword || 
        product.name.toLowerCase().includes(keyword.toLowerCase()) ||
        product.description.toLowerCase().includes(keyword.toLowerCase());
      
      const matchesCategory = !categoryId || product.categoryId === categoryId;
      
      return matchesKeyword && matchesCategory;
    });
  }

  static getHotProducts(): Product[] {
    const products = Storage.get<Product[]>(STORAGE_KEYS.PRODUCTS) || mockProducts;
    return products.filter(product => product.isHot);
  }

  static getProductsByCategory(categoryId: string): Product[] {
    const products = Storage.get<Product[]>(STORAGE_KEYS.PRODUCTS) || mockProducts;
    return products.filter(product => product.categoryId === categoryId);
  }
}
